package com.dongjiantong.app.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.FarmStock;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.farmrecords.dto.StatisticalDto;
import com.dongjiantong.farmrecords.enums.PurchaseEnum;
import com.dongjiantong.farmrecords.model.FarmRecordsInspection;
import com.dongjiantong.farmrecords.model.FarmRecordsPurchase;
import com.dongjiantong.farmrecords.service.*;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.user.service.FarmsService;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 界面 -- 我的 数据统计
 * @author Lee
 * @date 2018/1/19
 */
@Slf4j
@OpenBean
public class StatisticalController {

    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmsService farmsService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmStockService farmStockService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmRecordsProductionService farmRecordsProductionService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmRecordsFeedService farmRecordsFeedService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmRecordsDrugsService farmRecordsDrugsService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmRecordsImmunizationService farmRecordsImmunizationService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmRecordsPurchaseService farmRecordsPurchaseService;
    @Reference(version = "1.0.0",retries = 0,timeout = 6000)
    private FarmRecordsIntroduceService farmRecordsIntroduceService;

    /**
     * 我的界面获取统计数据
     * @param farmId    养殖场id
     * @param date  查询时间
     * @param type  1.本日 2.本周 3.本月 4.其他
     * @return
     */
    @OpenMethod(key = "app.myInfo.statistical.obtainStatistical",
                            paramNames = {"farmId","date","type"},
                            httpMethods = RequestMethod.GET)
    public Response<StatisticalDto> obtainStatistical(Long farmId,
                                                      String date,
                                                      Integer type) {
        StatisticalDto statisticalDto = new StatisticalDto();

        if (farmId == null) { throw new OPServerException("未获取到养殖场 id"); }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date date1 = null;
        if (!Strings.isNullOrEmpty(date)) { try { date1 = sdf.parse(date); } catch (Exception e) { e.printStackTrace(); } }

        //开始日期
        Date startDate = null;
        //结束日期
        Date endDate = null;

        if (type != null) {
            switch (type) {
                case 1:
                    //本日
                    startDate = date1;
                    endDate = date1;
                    break;
                case 2:
                    //本周
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(date1);
                    calendar1.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
                    startDate = calendar1.getTime();
                    endDate = date1;
                    break;
                case 3:
                    //本月
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(date1);
                    calendar2.set(Calendar.DAY_OF_MONTH,1);
                    startDate = calendar2.getTime();
                    endDate = date1;
                    break;
                case 4:
                    //其他 (整月)
                    Calendar calendar3 = Calendar.getInstance();
                    calendar3.setTime(date1);
                    calendar3.set(Calendar.DATE,calendar3.getActualMaximum(Calendar.DATE));
                    startDate = date1;
                    endDate = calendar3.getTime();
                    break;
            }
        }

        //todo 获取真实存栏和设置真实存栏
        FarmStock farmStock = farmStockService.selectByFarmID(farmId);
        if (farmStock != null) {
            statisticalDto.setRealStock(farmStock.getReal_stock());
        }

        //todo 根据日期获取出生,卖出和死淘数
        Map<String,Object> quantity = farmRecordsProductionService.selectQuantityByFarmId(farmId,startDate,endDate);
        if (quantity != null) {
            statisticalDto.setStock(quantity);
        }

        //todo 根据养殖场id 获取到的生产统计
        //养殖场的饲料消耗
        Float feedConsume = farmRecordsFeedService.selectTotalQuantity(farmId,startDate,endDate);
        statisticalDto.setFeedConsume(feedConsume);

        //兽药次数
        Integer drugCount = farmRecordsDrugsService.selectCountByFarmId(farmId,startDate,endDate);
        statisticalDto.setDrugCount(drugCount);

        //免疫次数
        Integer immunizationCount = farmRecordsImmunizationService.selectCountByFarmId(farmId,startDate,endDate);
        statisticalDto.setImmunizationCount(immunizationCount);

        //todo 根据养殖场id 获取到的采购统计

        //原料购置数量和总价
        Map<String,Object> materialInfo = farmRecordsPurchaseService.selectQuantityAndPrice(farmId, PurchaseEnum.MATERIAL.getCode(),startDate,endDate);
        if (materialInfo == null) { materialInfo = this.setMap(); }
        Double materialPrice = (Double) materialInfo.get("totalPrice");
        statisticalDto.setMaterialPurchase(materialInfo);

        //药物购置数量和总价
        Map<String,Object> drugInfo = farmRecordsPurchaseService.selectQuantityAndPrice(farmId,PurchaseEnum.DRUG.getCode(),startDate,endDate);
        if (drugInfo == null) { drugInfo = this.setMap(); }
        Double drugPrice = (Double) drugInfo.get("totalPrice");
        statisticalDto.setDrugPurchase(drugInfo);

        //疫苗购置数量和总价
        Map<String,Object> vaccineInfo = farmRecordsPurchaseService.selectQuantityAndPrice(farmId,PurchaseEnum.VACCINE.getCode(),startDate,endDate);
        if (vaccineInfo == null) { vaccineInfo = this.setMap(); }
        Double vaccinePrice = (Double) vaccineInfo.get("totalPrice");
        statisticalDto.setVaccinePurchase(vaccineInfo);

        //其他购置数量和总价
        Map<String,Object> otherInfo = farmRecordsPurchaseService.selectQuantityAndPrice(farmId,PurchaseEnum.OTHER.getCode(),startDate,endDate);
        if (otherInfo == null) { otherInfo = this.setMap(); }
        Double otherPrice = (Double) otherInfo.get("totalPrice");
        statisticalDto.setOtherPurchase(otherInfo);

        //引种记录数量和总价
        Map<String,Object> introduceInfo = farmRecordsIntroduceService.selectQuantityAndPrice(farmId,startDate,endDate);
        if (introduceInfo == null) { introduceInfo = this.setMap(); }
        Double introducePrice = (Double) introduceInfo.get("totalPrice");
        statisticalDto.setIntroducePurchase(introduceInfo);

        //todo 采购总金额
        Double totalPrice = materialPrice + drugPrice + vaccinePrice + introducePrice + otherPrice;
        statisticalDto.setTotalPrice(totalPrice);

        return Response.ok(statisticalDto);
    }

    private Map<String,Object> setMap(){
        Map<String,Object> map = Maps.newHashMap();
        map.put("quantity",0);
        map.put("totalPrice",0.00);
        return map;
    }
}
